#include "stdafx.h"
#include "wf_polylinetrim.h"
#include "pf_polyline.h"
#include "pf_snapper.h"



WF_PolylineTrim::WF_PolylineTrim(PF_Container& container,
	PF_GraphicView& graphicView)
	:PF_PreviewActionInterface("Trim segments",
			   container, graphicView) {}


QAction* WF_PolylineTrim::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	QAction* action = new QAction(tr("&Trim segments"), NULL);
	action->setShortcut(QKeySequence());
	action->setStatusTip(tr("Trim polyline's segments"));
	return action;
}

void WF_PolylineTrim::init(int status) {
	PF_ActionInterface::init(status);
	delEntity = Segment1 = Segment2 = NULL;
}



void WF_PolylineTrim::trigger() {

	PF_DEBUG->print("PF_ActionPolylineTrim::trigger()");

	if (delEntity!=NULL && Segment1->isAtomic() && Segment2->isAtomic()) {

		delEntity->setHighlighted(false);
		graphicView->drawEntity(delEntity);

		PF_Modification m(*container, graphicView);
		delEntity = m.polylineTrim((PF_Polyline&)*delEntity, *Segment1, *Segment2 );

//		delEntity = NULL;
		Segment1 = Segment2 = NULL;
		setStatus(SetSegment1);

		PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
	}
////////////////////////////////////////2006/06/15
	graphicView->redraw();
////////////////////////////////////////
}



void WF_PolylineTrim::mouseMoveEvent(PF_MouseEvent* e) {
	PF_DEBUG->print("PF_ActionPolylineTrim::mouseMoveEvent begin");

	switch (getStatus()) {
	case ChooseEntity:
	break;

	case SetSegment1:
	case SetSegment2:
		snapPoint(e);
		break;

	default:
		break;
	}

	PF_DEBUG->print("PF_ActionPolylineTrim::mouseMoveEvent end");
}



void WF_PolylineTrim::mouseReleaseEvent(PF_MouseEvent* e) {
        if (e->button()==Qt::LeftButton) {
		PF_Vector cPoint;
		switch (getStatus()) {
		case ChooseEntity:
			delEntity = catchEntity(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (delEntity->rtti()!=LX_Define::EntityPolyline) {
				PF_DIALOGFACTORY->commandMessage(
					tr("Entity must be a polyline."));
			} else {
				delEntity->setHighlighted(true);
				graphicView->drawEntity(delEntity);
				setStatus(SetSegment1);
////////////////////////////////////////2006/06/15
				graphicView->redraw();
////////////////////////////////////////
			}
			break;

		case SetSegment1:
			cPoint = snapPoint(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (!cPoint.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Specifying point is invalid."));
			} else if (!delEntity->isPointOnEntity(cPoint)) {
				PF_DIALOGFACTORY->commandMessage(
					tr("No Segment found on entity."));
			}else{
				Segment1 = NULL;
					double dist = graphicView->toGraphDX(snapRange)*0.9;
				Segment1 =  (PF_AtomicEntity*)((PF_Polyline*)delEntity)->getNearestEntity( PF_Vector(graphicView->toGraphX(e->x()),
				 graphicView->toGraphY(e->y())), &dist, LX_Define::ResolveNone);
				if(Segment1 == NULL)
					break;
				setStatus(SetSegment2);
			}
			break;
		case SetSegment2:
			cPoint = snapPoint(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (!cPoint.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Specifying point is invalid."));
			} else if (!delEntity->isPointOnEntity(cPoint)) {
				PF_DIALOGFACTORY->commandMessage(
					tr("No Segment found on entity."));
			}else{
				Segment2 = NULL;
					double dist = graphicView->toGraphDX(snapRange)*0.9;
				Segment2 =  (PF_AtomicEntity*)((PF_Polyline*)delEntity)->getNearestEntity( PF_Vector(graphicView->toGraphX(e->x()),
				 graphicView->toGraphY(e->y())), &dist, LX_Define::ResolveNone);
				if(Segment2 == NULL)
					break;
				deleteSnapper();
				trigger();
			}
			break;

		default:
			break;
		}
        } else if (e->button()==Qt::RightButton) {
		deleteSnapper();
		if (delEntity!=NULL) {
			delEntity->setHighlighted(false);
			graphicView->drawEntity(delEntity);
////////////////////////////////////////2006/06/15
			graphicView->redraw();
////////////////////////////////////////
		}
		init(getStatus()-1);
	}
}


void WF_PolylineTrim::updateMouseButtonHints() {
	switch (getStatus()) {
	case ChooseEntity:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify polyline to trim"),
							tr("Cancel"));
		break;
	case SetSegment1:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first segment"),
							tr("Back"));
		break;
	case SetSegment2:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second segment"),
							tr("Back"));
		break;
	default:
		PF_DIALOGFACTORY->updateMouseWidget("", "");
		break;
	}
}



void WF_PolylineTrim::updateMouseCursor() {
	graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_PolylineTrim::updateToolBar() {
	switch (getStatus()) {
	case SetSegment1:
	case SetSegment2:
	case ChooseEntity:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
		break;
	default:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarPolylines);
		break;
	}
}


// EOF
